package com.le.tester.book.effectivejava3rdchinese.secondReading.l28;


import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
 * createTime：2024-09-06 14:13
 * description：泛型中，列表要优于数组，但是在泛型类型中使用列表并不总是可行或可取的
 */
public class ChooserV3<T> {
    private final List<T> choiceList;

    //    public ChooserV3(Collection<T> choices) {
//        choiceList = new ArrayList<>(choices);
//    }
    //生产者
    public ChooserV3(Collection<? extends T> choices) {
        choiceList = new ArrayList<>(choices);
    }


    public T choose() {
        Random random = ThreadLocalRandom.current();
        return choiceList.get(random.nextInt(choiceList.size()));

    }
}
